//VARIABLES

//FUCNTIONS
function show(elem){
    elem.removeClass("hidden");
    elem.addClass("show");
}

function hide(elem){
    elem.removeClass("show");
    elem.addClass("hidden");
}

function showInfoUser(baseUrl){
        $.ajax({
                type: "GET",
                url: baseUrl+"ClientController/ajaxInfoUser",
                dataType: "json"
            }).done(function( infoUser ) {
                if(infoUser){
                    $("#infoNom").html(infoUser.nom);
                    $("#infoPrenom").html(infoUser.prenom);
                    $("#infoAdresse").html(infoUser.adresse);
                    $("#infoMail").html(infoUser.mail);
                    $("#infoId").attr("value",infoUser.id);
                    show($(".infoClient"));                    
                    hide($("#linkConnexion"));
                    hide($("#linkInscription"));
                    $("#boutonValiderCommande").attr('disabled', false);
                }else{
                    hide($(".infoClient"));
                    show($("#linkConnexion"));
                    show($("#linkInscription"));
                    $("#boutonValiderCommande").attr('disabled', true);
                }
            });
}

function isValidEmail(email) {
    var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
    // alert( pattern.test(emailAddress) );
    return pattern.test(email);
};

function checkConnexionForm(){
            
            var bool = true
            $( ".required" ).each(function( index ) {
                bool = ($(this).val()=="")?false:true;
            });
            bool = isValidEmail($("#mail").val());
            if(bool){
               $("#buttonValiderInscription").attr('disabled', false);
            }else{
               $("#buttonValiderInscription").attr('disabled', true);               
            }
}

function showError(field,cond){
    if(cond){
       field.removeClass( " has-success" ).addClass( "has-error" );
   }else{
       field.removeClass( "has-error" ).addClass( "has-success" );
   }
}

function showAlert(msg, msgClass, buttonOkFunction){
     $('#alertModal').modal('show');
     $('#alertMsg')
             .removeClass('alert-success alert-info alert-warning alert-danger')
             .addClass(msgClass)
             .html(msg);
     $('#buttonAlertOk').click(buttonOkFunction?buttonOkFunction:function(){
        $('#alertModal').modal('hide');         
     });
}